package com.yogee.topseller.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.DingyueListActivity;
import com.yogee.topseller.activity.PayActivity;
import com.yogee.topseller.activity.ProductDetailsActivity;
import com.yogee.topseller.bean.DingyueBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 订阅adapter
 * @auathor ${XiaoJianghong}
 * @date 2017/6/26.
 */

public class DingyueAdapter extends RecyclerView.Adapter<DingyueAdapter.ItemHolder> {

    private Activity context;
    private List<DingyueBean.DingyueInfo> beans = new ArrayList<>();
    private List<DingyueBean.DingyueInfo> selectBeans = new ArrayList<>();
    private boolean flag = false;

    private PayActivity.Listener listener;

    public DingyueAdapter(Activity context) {
        this.context = context;
    }

    public void addData(List<DingyueBean.DingyueInfo> beans) {
        this.beans = beans;
        notifyDataSetChanged();
    }
    public void setEdit(boolean flag){
        this.flag = flag;
        notifyDataSetChanged();
    }
    public void setSelectAll(boolean selectAll){
        if(beans!=null&&beans.size()>0){
            for(int i =0;i<beans.size();i++){
                beans.get(i).setSelect(selectAll);
            }
            notifyDataSetChanged();
        }

    }

    public List<String> getSelect(){
        selectBeans.clear();
        List<String> stringList = new ArrayList<>();
        for(int i = 0;i<beans.size();i++){
            if(beans.get(i).isSelect()){
                stringList.add(beans.get(i).getId());
                selectBeans.add(beans.get(i));
            }
        }
        return stringList;
    }

    public void setListener(PayActivity.Listener listener){
        this.listener = listener;
    }

    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_product_dingyue,parent,false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(final ItemHolder holder, final int position) {
        final DingyueBean.DingyueInfo bean = beans.get(position);
        if(flag){
            holder.cb_select.setVisibility(View.VISIBLE);
            holder.select_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(bean.isSelect()){
                        beans.get(position).setSelect(false);
                        holder.cb_select.setImageResource(R.drawable.unselect1);
                    }else{
                        beans.get(position).setSelect(true);
                        holder.cb_select.setImageResource(R.drawable.select1);
                    }
                }
            });
        }else{
            holder.cb_select.setVisibility(View.GONE);
            holder.select_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    ToastUtils.showToast(context,"产品详情");
                    context.startActivity(new Intent(context, ProductDetailsActivity.class).putExtra("id",bean.getProId()).putExtra("isFast", "0"));
                }
            });
        }
        if(bean.isSelect()){
            holder.cb_select.setImageResource(R.drawable.select1);
        }else{
            holder.cb_select.setImageResource(R.drawable.unselect1);
        }
        holder.tv_product_name.setText(bean.getName());
        if("0".equals(bean.getNewstime())){
            holder.tv_3.setText("企业咨讯暂无更新");
            holder.tv_4.setText("");
            holder.tv_info2.setText("");
            holder.ll2.setVisibility(View.GONE);
        }else{
            holder.tv_info2.setText(bean.getNewstime());
            holder.tv_3.setText("前更新了产品");
            holder.tv_4.setText("企业咨讯");
            holder.ll2.setVisibility(View.VISIBLE);
        }

        if(bean.getTime().equals("0")){
            holder.tv_1.setText("实时信息暂无更新");
            holder.tv_2.setText("");
            holder.tv_info1.setText("");
            holder.ll_ssxx.setVisibility(View.GONE);
        }else{
            holder.tv_info1.setText(bean.getTime());
            holder.tv_1.setText("前更新了产品");
            holder.tv_2.setText("实时信息");
            holder.ll_ssxx.setVisibility(View.VISIBLE);
        }

        if("0".equals(bean.getNewstime())&&bean.getTime().equals("0")){
            holder.tv_1.setText("实时信息暂无更新");
            holder.tv_2.setText("");
            holder.tv_info1.setText("");
            holder.ll2.setVisibility(View.GONE);
            holder.ll_ssxx.setVisibility(View.VISIBLE);
        }
        // 删除选中项
        holder.img_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ((DingyueListActivity)context).dingyueDelete(bean.getId(),position);
            }
        });

    }

    /**
     * 移除单个项
     * @param pos
     */
    public void romoveSelect(int pos){
        beans.remove(pos);
        notifyItemRemoved(pos);
        notifyItemRangeChanged(pos, getItemCount());
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);//休息

                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void shuaxin(){
        List<DingyueBean.DingyueInfo> newList = new ArrayList<>();
        for(int i=0;i<beans.size();i++){
            if(!selectBeans.contains(beans.get(i))){
                newList.add(beans.get(i));
            }
        }
        beans.clear();
        beans.addAll(newList);
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return beans == null ? 0 : beans.size();
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_product_name)
        TextView tv_product_name;
        @BindView(R.id.img_delete)
        ImageView img_delete;
        @BindView(R.id.tv_info1)
        TextView tv_info1;
        @BindView(R.id.tv_info2)
        TextView tv_info2;
        @BindView(R.id.cb_select)
        ImageView cb_select;
        @BindView(R.id.select_ll)
        LinearLayout select_ll;
        @BindView(R.id.tv_1)
        TextView tv_1;
        @BindView(R.id.tv_2)
        TextView tv_2;
        @BindView(R.id.tv_3)
        TextView tv_3;
        @BindView(R.id.tv_4)
        TextView tv_4;
        @BindView(R.id.ll_ssxx)
        LinearLayout ll_ssxx;
        @BindView(R.id.ll2)
        LinearLayout ll2;


        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}